    <template>
        <div class="app-container">
    
    
        <!--查询表单-->
    <div class="search-div">
            <el-form label-width="70px" size="small">
            <el-row>
                <!-- 条件查询 -->
                <el-col :span="6">
                <el-form-item label="关键字"  label-width="100px">
                    <el-input style="width: 100%" v-model="searchObj.keyword" placeholder="车牌号"></el-input>
                </el-form-item>
                </el-col>
                
                <!-- 创建时间查询 -->
                <el-col :span="6">
                    <el-form-item label="操作时间"  label-width="100px">
                        <el-date-picker
                        v-model="createTimes"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        style="margin-right: 10px; width: 100%"
                        />
                    </el-form-item>
                </el-col>

                <el-col :span="6" >
                <el-form-item label="车主类别" label-width="100px">
                    <template>
                        <el-select v-model="searchObj.ownerType" placeholder="请选择"  style="margin-right: 10px; width: 100%">
                            <el-option
                            v-for="item in CarTypes"
                            :key="item.id"
                            :label="item.value"
                            :value="item.id">
                            </el-option>
                        </el-select>
                </template>
               </el-form-item>
             </el-col>
        
            </el-row>
            <el-row style="display:flex">
                <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchData()">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
                
            </el-row>
            </el-form>
        </div>
    
            <!-- 工具条 -->
    <div class="tools-div">
        <el-button type="success" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
    </div>
    
        <!-- 表格 -->
        <el-table
            v-loading="listLoading"
            :data="list"
            stripe
            border
            style="width: 100%;margin-top: 10px;">
    
            <el-table-column
            label="序号"
            width="70"
            align="center">
            <template slot-scope="scope">
                {{ (page - 1) * limit + scope.$index + 1 }}
            </template>
            </el-table-column>
            <!-- <el-table-column prop="param.community_name" label="小区名称" /> -->
            <!-- <el-table-column prop="param.owner_name" label="车主姓名" /> -->
           
            <el-table-column
                prop="picture"
                label="车辆照片">
            <template slot-scope="scope">
                <viewer>
                <img :src="scope.row.picture" alt="" style="height:80px ;">
                </viewer>
            </template>
            </el-table-column>
            
            <el-table-column prop="color" label="车辆颜色" />
            <el-table-column prop="carNumber" label="车牌号" />
            <el-table-column prop="ownerType" label="车主类型" >
                <template slot-scope="scope">
                    <el-tag type="warning" v-if="scope.row.ownerType===0">业主</el-tag>
                    <el-tag type="primary" v-if="scope.row.ownerType===1">物业人员</el-tag>
                    <el-tag type="danger" v-if="scope.row.ownerType===2">外来人员</el-tag>

            </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
    
            <el-table-column prop="createTime" label="创建时间" width="160"/>
            <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)" title="修改"/>
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"/>
            </template>
            </el-table-column>
        </el-table>
    
        <!--  添加/修改-->
        <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%" >
            <el-form ref="dataForm" :model="sysCar" label-width="150px" size="small" style="padding-right: 40px;">

            <!-- <el-form-item label="车辆照片">
                <el-input v-model="sysCar.picture"/>
            </el-form-item> -->
            <el-form-item label="车辆照片" prop="picture">
                <el-upload class="upload-demo" name="uploadImage" action="http://localhost:8085/admin/system/upload/uploadImage"
                :limit="1"
                :on-success="handleImageSuccess"
                :before-upload="handleBeforeUpload"
                :show-file-list="false"
                >
                    <el-button type="primary" size="small">点击上传</el-button>
                </el-upload>
            </el-form-item>

            <el-form-item label="车辆颜色">
                <el-input v-model="sysCar.color"/>
            </el-form-item>
            <el-form-item label="车牌号">
                <el-input v-model="sysCar.carNumber"/>
            </el-form-item>
            <el-form-item label="车主类型">
                <el-radio-group v-model="sysCar.ownerType" >
                    <el-radio :label="0">业主</el-radio>
                    <el-radio :label="1">物业人员</el-radio>
                    <el-radio :label="2">外来人员</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="sysCar.remark"/>
            </el-form-item>
            <el-form-item label="创建时间" v-if="sysCar.id">
                <el-input v-model="sysCar.createTime" disabled/>
            </el-form-item>
            <!-- 备注 -->
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
            <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
            </span>
        </el-dialog>
    
                <!-- 分页组件 -->
        <el-pagination
        :current-page="page"
        :total="total"
        :page-size="limit"
        style="padding: 30px 0; text-align: center;"
        layout="total, prev, pager, next, jumper"
        @current-change="fetchData"
        />
        </div>
    </template>
    
    <script>
    import api from '@/api/sysParking/car'
    export default {
        data(){
            return {
    
            listLoading:true,
            list:[],
            total:0,
            page:1,
            limit:3,
            searchObj:{},
            sysCar:{},
            dialogVisible : false,
            carTpye:'',
            CarTypes:[
        // ( 0:业主 1:物业人员 2:外来人员)
                {
                    id:0,
                    value:'业主',
                },
                {
                    id: 1,
                    value: '物业人员',
                },
                {
                    id: 2,
                    value: '外来人员',
                },
            ]}
            
        },
        created(){
            this.fetchData(1);
        },
        methods:{

                 //文件开始上传,开始屏幕遮罩
            handleBeforeUpload() {
            this.listLoading = true;
            },
            //图片上传成功的钩子函数
            handleImageSuccess(res, file) {

                console.log(res);  // 得到的是图片地址
                console.log(file);  // 得到是一个对象  
                console.log(file.response); 
                this.listLoading = false;
                if (file.response != "") {
                    // this.sysCommunity.image = file.response.data;
                    this.sysCar.picture = file.response;
                    console.log(this.sysCar.picture);

                    this.$message({
                    type: "info",
                    message: "图片上传成功",
                    duration: 6000,
                    });
                } else {
                    this.$message({
                    type: "info",
                    message: "图片上传失败",
                    duration: 6000,
                    });
                }
            },

            //得到数据
            fetchData(page=1){
                    this.listLoading = true; 
                    this.page = page;
                    if (this.createTimes && this.createTimes.length == 2) {
                        this.searchObj.createTimeBegin = this.createTimes[0];
                        this.searchObj.createTimeEnd = this.createTimes[1];
                    }
                
                api.findPageInfo(this.page,this.limit,this.searchObj).then(res=>{
                    //    console.log(res.data)
                    this.list = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
            },
    
            //重置按钮
            resetData(){
                // 清空表单
                this.searchObj={};
                // 再次刷新列表数据
                this.fetchData();
            },
            //修改按钮
            edit(id){
            api.findCarById(id).then(res=>{
                // console.log(res)
                this.sysCar = res.data
                //弹出弹框
                this.openDialog();
            })
            },
            //添加按钮
            add(){
            this.sysCar = {};
            this.openDialog();
            },
            //开启弹框 查询选择框列表 
        openDialog(){
            this.dialogVisible = true;
        },
         removeDataById(id){
            this.$confirm('此操作将永久去除该车辆, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 点击确定
                api.removeCarById(id).then(response=>{
                    // 1.给出提示
                    this.$message({
                type: 'success',
                message: '删除成功!'
                });
                this.listLoading = true;
                    //2. 再次刷新数据列表
                    this.fetchData()
                })
    
                
            })
            },
            //保存/修改
            saveOrUpdate(){
            if(this.sysCar.id){
                //修改
                this.update();
            }else{
                this.addCar();
            }
            },
    
            update(){
            api.updateCar(this.sysCar).then(res=>{
                    // 1.给出提示
                    this.$message({
                type: 'success',
                message: '修改成功!'
                });
                //刷新列表
                this.fetchData();
                //关闭弹框
                this.dialogVisible = false;
            })
            },
    
            addCar(){
            api.addCar(this.sysCar).then(res=>{
                    // 1.给出提示
                    this.$message({
                type: 'success',
                message: '新增成功!'
                });
                //刷新列表
                this.fetchData();
                //关闭弹框
                this.dialogVisible = false;
            })
            
            },
    
        }
    
    }
    </script>
    
    <style>
    
    </style>